package com.rgs.se.java8new.mystream;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author Ren Gaoshuai
 * @ClassName TestMain
 * @Description stream 新特性测试
 * 转list
 * 转set
 * 转map
 * 最小最大平均
 * 求和
 * 分组统计
 * @date 2022-02-24 10:06
 **/
public class TestMain {

    public static void main(String[] args) {
        test();
    }


    public static void test(){
        List<Integer> list1 = new ArrayList<>();
        list1.add(1);
        list1.add(3);
        list1.add(4);


        List<Integer> collectList = Stream.of(1, 3, 4).collect(Collectors.toList());
        Set<Integer> collect2Set = Stream.of(1, 1, 2, 3, 3).collect(Collectors.toSet());
        Collection<Integer> collect3Collection = Stream.of(2, 3, 1).collect(Collectors.toCollection(TreeSet::new));
        Map<Integer, Integer> collect4Map = Stream.of(1, 3, 4).collect(Collectors.toMap(x -> x, x -> x + 1));

        //最小值

        Optional<Integer> collect5Min = list1.stream().min(Integer::compare);

        //平均值
        Double collect6Averag = list1.stream().collect(Collectors.averagingInt(x -> x));

        //求和
        Integer collect = list1.stream().collect(Collectors.summingInt(x -> x));

        //分组
        //最简单分组，对集合元素进行分组
        Map<Integer, List<Integer>> collect1 = list1.stream().collect(Collectors.groupingBy(x -> x));
        //分组+统计
        Map<Integer, Long> collect2 = list1.stream().collect(Collectors.groupingBy(x -> x, Collectors.counting()));

        //拼接字符串
        String collect3 = Stream.of("a", "b", "c").collect(Collectors.joining("-"));

    }
}
